package isjinhao.jlox.lexical;

import isjinhao.jlox.Lox;
import isjinhao.jlox.grammar.ParseError;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author ISJINHAO
 * @Date 2021/2/23 15:35
 */
public class TokenSequence extends Sequence<List<Token>, Token> {

    public TokenSequence() {
        super(new ArrayList<>(), new Token(TokenType.EOF, "", null, -1));
    }

    public List<Token> getSource() {
        return source;
    }

    public void addItem(Token token) {
        source.add(token);
        length++;
    }

    @Override
    protected int length() {
        return source.size();
    }

    @Override
    protected boolean equalSequenceItem(Token c1, Token c2) {
        return c1.getType() == c2.getType();
    }

    @Override
    protected Token indexOf(int index) {
        return source.get(index);
    }

    public Token consume(TokenType type, String message) {
        if (curEqual(type)) {
            advance();
            return takeCur();
        }
        throw error(takeCur(), message);
    }

    public ParseError error(Token token, String message) {
        Lox.error(token, message);
        return new ParseError(message);
    }

    public boolean curEqual(TokenType ... types) {
        for (TokenType type : types) {
            if (curEqual(wrapTokenType(type))) {
                return true;
            }
        }
        return false;
    }

    private Token wrapTokenType(TokenType tokenType) {
        return new Token(tokenType);
    }



}